package com.song.tox_demo.main;

import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.LocationSource;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.UiSettings;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.amap.api.maps2d.model.MyLocationStyle;
import com.song.tox_demo.R;
import com.song.tox_demo.base.BaseActivity;

public class MainActivity extends BaseActivity implements LocationSource, AMapLocationListener, AMap.OnMarkerClickListener,View.OnClickListener{

    private DrawerLayout drawer_layout;
    private MapView mapView;
    private AMap aMap;
    private static final int STROKE_COLOR = Color.argb(180, 3, 145, 255);
    private static final int FILL_COLOR = Color.argb(10, 0, 0, 180);
    private AMapLocationClientOption mLocationOption;
    private AMapLocationClient mlocationClient;
    private OnLocationChangedListener mListener;
    private MarkerOptions markerOption;
    private TextView aaa;
    private TextView bbb;
    private TextView ccc;
    private UiSettings mUiSettings;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById();
        /*
            初始化地图
         */
        initMap(savedInstanceState);
    }

    private void findViewById() {
        drawer_layout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mapView = (MapView) findViewById(R.id.aMap);
        aaa = (TextView) findViewById(R.id.aaa);
        bbb = (TextView) findViewById(R.id.bbb);
        ccc = (TextView) findViewById(R.id.ccc);

        drawer_layout.setDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                ShowToast(String.valueOf(mUiSettings.isZoomGesturesEnabled()));
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                mUiSettings.setAllGesturesEnabled(false);
                ShowToast(String.valueOf(mUiSettings.isZoomGesturesEnabled()));
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                mUiSettings.setAllGesturesEnabled(true);
                ShowToast(String.valueOf(mUiSettings.isZoomGesturesEnabled()));
            }

            @Override
            public void onDrawerStateChanged(int newState) {

                ShowToast(String.valueOf(mUiSettings.isZoomGesturesEnabled()));

            }
        });
    }

    private void initMap(Bundle savedInstanceState) {
        mapView.onCreate(savedInstanceState);
        if (aMap == null) {
            aMap = mapView.getMap();
            mUiSettings = aMap.getUiSettings();
            /*
                设置地图变量
             */
            setUpMap();
        }
        aMap.clear();
    }

    private void setUpMap() {
        aMap.setLocationSource(this);// 设置定位监听
        aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示
        aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位，false表示隐藏定位层并不可触发定位，默认是false

        // 设置定位的类型为定位模式 ，可以由定位、跟随或地图根据面向方向旋转几种
//        aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
        //自定义mark商家
        aMap.setOnMarkerClickListener(this);// 设置点击marker事件监听器
        /*
            定位标识风格
         */
        setupLocationStyle();
    }

    private void setupLocationStyle() {
        // 自定义系统定位蓝点
        MyLocationStyle myLocationStyle = new MyLocationStyle();
        // 自定义定位蓝点图标
        myLocationStyle.myLocationIcon(BitmapDescriptorFactory.
                fromResource(R.mipmap.gps_point));
        // 自定义精度范围的圆形边框颜色
        myLocationStyle.strokeColor(STROKE_COLOR);
        //自定义精度范围的圆形边框宽度
        myLocationStyle.strokeWidth(2);
        // 设置圆形的填充颜色
        myLocationStyle.radiusFillColor(FILL_COLOR);
        // 将自定义的 myLocationStyle 对象添加到地图上
        aMap.setMyLocationStyle(myLocationStyle);
    }




    @Override
    public void onLocationChanged(AMapLocation amapLocation) {
        if (mListener != null && amapLocation != null) {

            if (amapLocation.getErrorCode() == 0) {
                mlocationClient.stopLocation();

                mListener.onLocationChanged(amapLocation);// 显示系统小蓝点
                Log.e("AmapErr", "定位成功");

//                aMap.moveCamera(CameraUpdateFactory.changeLatLng(new LatLng(amapLocation.getLatitude(), amapLocation.getLongitude())));
                aMap.moveCamera(CameraUpdateFactory.zoomTo(17));
//                latitude = amapLocation.getLatitude();
//                longitude = amapLocation.getLongitude();
                /*
                    获取车辆接口
                 */
                getCarData();

            } else {
                String errText = "定位失败," + amapLocation.getErrorCode() + ": " + amapLocation.getErrorInfo();
                Log.e("AmapErr", errText);
                Toast.makeText(this, "定位失败", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void getCarData() {


    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        return false;
    }

    @Override
    public void activate(OnLocationChangedListener onLocationChangedListener) {
        mListener = onLocationChangedListener;
        if (mlocationClient == null) {
            mlocationClient = new AMapLocationClient(this);
            mLocationOption = new AMapLocationClientOption();
            //设置定位监听
            mlocationClient.setLocationListener(this);
            //设置为高精度定位模式
            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
            //设置定位参数
            mlocationClient.setLocationOption(mLocationOption);
            // 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
            // 注意设置合适的定位时间的间隔（最小间隔支持为2000ms），并且在合适时间调用stopLocation()方法来取消定位请求
            // 在定位结束后，在合适的生命周期调用onDestroy()方法
            // 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除
            mlocationClient.startLocation();
        }
    }

    @Override
    public void deactivate() {
        mListener = null;
        if (mlocationClient != null) {
            mlocationClient.stopLocation();
            mlocationClient.onDestroy();
        }
        mlocationClient = null;
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.aaa:
                ShowToast("aaa");
                break;
            case R.id.bbb:
                ShowToast("bbb");
                break;
            case R.id.ccc:
                ShowToast("ccc");
                break;
        }
    }


}
